/* 
 * Gneat (http://gneat.org)
 *
 * (C) Copyright 2011 and beyond - Claudio Stella, all rights reserved.
 *
 * This file is part of Gneat.
 *
 * Gneat is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License (LGPL)
 * as published by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gneat is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * and the GNU General Public License, for completeness, along with Gneat.  
 * If not, see <http://www.gnu.org/licenses/>.
 */
package org.neatshell.gneat.client.event.live.mouse;

import org.neatshell.gneat.client.event.live.DomEvent;
import org.neatshell.gneat.client.event.mouse.IMouseEvent;
import org.neatshell.gneat.client.handler.Handler;
import org.neatshell.gneat.client.presenter.core.Presenter;

import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;

public abstract class MouseEvent<H extends Handler> extends DomEvent<H>
		implements IMouseEvent<H> {
	public int getClientX() {
		return getNativeEvent().getClientX();
	}

	public int getClientY() {
		return getNativeEvent().getClientY();
	}

	public int getNativeButton() {
		return getNativeEvent().getButton();
	}

	public int getRelativeX(Element target) {
		NativeEvent e = getNativeEvent();

		return e.getClientX() - target.getAbsoluteLeft()
				+ target.getScrollLeft()
				+ target.getOwnerDocument().getScrollLeft();
	}

	@Override
	public int getRelativeX(Presenter target) {
		return getRelativeX((Element) target.getView().getNode());
	}

	public int getRelativeY(Element target) {
		NativeEvent e = getNativeEvent();

		return e.getClientY() - target.getAbsoluteTop() + target.getScrollTop()
				+ target.getOwnerDocument().getScrollTop();
	}
	
	@Override
	public int getRelativeY(Presenter target) {
		return getRelativeY((Element) target.getView().getNode());
	}

	public int getScreenX() {
		return getNativeEvent().getScreenX();
	}

	public int getScreenY() {
		return getNativeEvent().getScreenY();
	}

	public int getX() {
		Element relativeElem = getRelativeElement();
		if (relativeElem != null) {
			return getRelativeX(relativeElem);
		}
		return getClientX();
	}

	public int getY() {
		Element relativeElem = getRelativeElement();
		if (relativeElem != null) {
			return getRelativeY(relativeElem);
		}
		return getClientY();
	}
}